package com.youyi.yychosesdk.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.PointF;
import android.net.Uri;
import android.os.Build;
import android.support.v4.content.FileProvider;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.davemorrissey.labs.subscaleview.ImageSource;
import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView;
import com.github.chrisbanes.photoview.OnScaleChangedListener;
import com.github.chrisbanes.photoview.PhotoView;
import com.lkx.liandianqinew.R;
import com.youyi.yychosesdk.constant.Type;
import com.youyi.yychosesdk.models.album.entity.Photo;
import com.youyi.yychosesdk.setting.Setting;
import java.io.File;
import java.util.ArrayList;

public class PreviewPhotosAdapter extends RecyclerView.Adapter<PreviewPhotosAdapter.PreviewPhotosViewHolder> {
    private LayoutInflater inflater;
    private OnClickListener listener;
    private ArrayList<Photo> photos;

    public interface OnClickListener {
        void onPhotoClick();

        void onPhotoScaleChanged();
    }

    public PreviewPhotosAdapter(Context context, ArrayList<Photo> arrayList, OnClickListener onClickListener) {
        this.photos = arrayList;
        this.inflater = LayoutInflater.from(context);
        this.listener = onClickListener;
    }

    @Override // android.support.v7.widget.RecyclerView.Adapter
    public PreviewPhotosViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        return new PreviewPhotosViewHolder(this.inflater.inflate(R.layout.item_preview_photo_easy_photos, viewGroup, false));
    }

    public void onBindViewHolder(PreviewPhotosViewHolder previewPhotosViewHolder, int i) {
        final Uri uri = this.photos.get(i).uri;
        String str = this.photos.get(i).path;
        final String str2 = this.photos.get(i).type;
        double d = ((double) this.photos.get(i).height) / ((double) this.photos.get(i).width);
        previewPhotosViewHolder.ivPlay.setVisibility(View.GONE);
        previewPhotosViewHolder.ivPhotoView.setVisibility(View.GONE);
        previewPhotosViewHolder.ivLongPhoto.setVisibility(View.GONE);
        if (str2.contains(Type.VIDEO)) {
            previewPhotosViewHolder.ivPhotoView.setVisibility(View.VISIBLE);
            Setting.imageEngine.loadPhoto(previewPhotosViewHolder.ivPhotoView.getContext(), uri, previewPhotosViewHolder.ivPhotoView);
            previewPhotosViewHolder.ivPlay.setVisibility(View.VISIBLE);
            previewPhotosViewHolder.ivPlay.setOnClickListener(new View.OnClickListener() {
                /* class com.youyi.yychosesdk.ui.adapter.PreviewPhotosAdapter.AnonymousClass1 */

                public void onClick(View view) {
                    PreviewPhotosAdapter.this.toPlayVideo(view, uri, str2);
                }
            });
        } else if (str.endsWith(Type.GIF) || str2.endsWith(Type.GIF)) {
            previewPhotosViewHolder.ivPhotoView.setVisibility(View.VISIBLE);
            Setting.imageEngine.loadGif(previewPhotosViewHolder.ivPhotoView.getContext(), uri, previewPhotosViewHolder.ivPhotoView);
        } else if (d > 2.3d) {
            previewPhotosViewHolder.ivLongPhoto.setVisibility(View.VISIBLE);
            previewPhotosViewHolder.ivLongPhoto.setImage(ImageSource.uri(str));
        } else {
            previewPhotosViewHolder.ivPhotoView.setVisibility(View.VISIBLE);
            Setting.imageEngine.loadPhoto(previewPhotosViewHolder.ivPhotoView.getContext(), uri, previewPhotosViewHolder.ivPhotoView);
        }
        previewPhotosViewHolder.ivLongPhoto.setOnClickListener(new View.OnClickListener() {
            /* class com.youyi.yychosesdk.ui.adapter.PreviewPhotosAdapter.AnonymousClass2 */

            public void onClick(View view) {
                PreviewPhotosAdapter.this.listener.onPhotoClick();
            }
        });
        previewPhotosViewHolder.ivPhotoView.setOnClickListener(new View.OnClickListener() {
            /* class com.youyi.yychosesdk.ui.adapter.PreviewPhotosAdapter.AnonymousClass3 */

            public void onClick(View view) {
                PreviewPhotosAdapter.this.listener.onPhotoClick();
            }
        });
        previewPhotosViewHolder.ivLongPhoto.setOnStateChangedListener(new SubsamplingScaleImageView.OnStateChangedListener() {
            @Override // com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView.OnStateChangedListener
            public void onCenterChanged(PointF pointF, int i) {
            }

            @Override // com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView.OnStateChangedListener
            public void onScaleChanged(float f, int i) {
                PreviewPhotosAdapter.this.listener.onPhotoScaleChanged();
            }
        });
        previewPhotosViewHolder.ivPhotoView.setScale(1.0f);
        previewPhotosViewHolder.ivPhotoView.setOnScaleChangeListener(new OnScaleChangedListener() {
            /* class com.youyi.yychosesdk.ui.adapter.PreviewPhotosAdapter.AnonymousClass5 */

            @Override // com.github.chrisbanes.photoview.OnScaleChangedListener
            public void onScaleChange(float f, float f2, float f3) {
                PreviewPhotosAdapter.this.listener.onPhotoScaleChanged();
            }
        });
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void toPlayVideo(View view, Uri uri, String str) {
        Context context = view.getContext();
        Intent intent = new Intent("android.intent.action.VIEW");
        if (Build.VERSION.SDK_INT >= 24) {
           intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        }
        intent.setDataAndType(uri, str);
        context.startActivity(intent);
    }

    private Uri getUri(Context context, String str, Intent intent) {
        File file = new File(str);
        if (Build.VERSION.SDK_INT < 24) {
            return Uri.fromFile(file);
        }
       intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        return FileProvider.getUriForFile(context, Setting.fileProviderAuthority, file);
    }

    @Override // android.support.v7.widget.RecyclerView.Adapter
    public int getItemCount() {
        return this.photos.size();
    }

    public class PreviewPhotosViewHolder extends RecyclerView.ViewHolder {
        public SubsamplingScaleImageView ivLongPhoto;
        PhotoView ivPhotoView;
        ImageView ivPlay;

        PreviewPhotosViewHolder(View view) {
            super(view);
            this.ivLongPhoto = (SubsamplingScaleImageView) view.findViewById(R.id.iv_long_photo);
            this.ivPhotoView = (PhotoView) view.findViewById(R.id.iv_photo_view);
            this.ivPlay = (ImageView) view.findViewById(R.id.iv_play);
        }
    }
}
